<?php

function qorder1()
{

    header('Access-Control-Allow-Origin:*');
    $location = I("post.location");
    $uid = I("post.uid");
    $page = I("post.page");
    $pagesize = I("post.pagesize");
    $start = $page * $pagesize;

//存储符合条件的订单数组
    $orderList = [];

    if (empty($location)) {
        $this->myApiPrint('请填写你的店铺地址', 400);
    }

    if ($uid == "") {
        $message_count = 0;
        $this->myApiPrint('缺少请求参数uid！', 400);
    }

//用户相关消息统计
    $message_count = M("message")->where("message_status = 0 and message_uid = $uid")->count();

//读取系统设置
    $systemSet = M("setting")->where('setting_id=1')->find();
//系统设置的过期时间 24小时
    $validTime = $systemSet['setting_expiration_time'];
//系统设置的有效距离 20
    $validDistance = (float) $systemSet['setting_tintegral1'];

//所有待抢单订单
    $tempOrderList = M("qorder")
        ->where("qorder_token = 0 and qorder_send_order = 0")
        ->field("qorder_id,qorder_sn,qorder_name,qorder_phone,qorder_project,qorder_content,qorder_time,qorder_price,qorder_address,qorder_d,qorder_product_type,
qorder_mprice")
        ->order("qorder_time desc")
        ->limit($start, $pagesize)
        ->select();
//当前师傅店铺
    $shop = M("shop")
        ->where("shop_uid=" . $uid)
        ->field("shop_id,shop_coorp,shop_address,shop_uid,shop_ppa")
        ->find();
//当前师傅可以接单的产品类型数组
    $shop_ppa_array = explode(",", $shop['shop_ppa']);

    foreach ($tempOrderList as $order) {

//1.先判断订单是否过期
        $orderTime = $order['qorder_time'];
//订单过期时间
        $expireTime = (int) strtotime($orderTime) + (3600 * (int) $validTime);
        $nowTime = time();
        if ($nowtime >= $expireTime) {
            M('qorder')->where("qorder_id=" . $order['qorder_id'])->save(array("qorder_token" => 8));
            continue;
        }
        $order['qorder_remaining_time'] = $expireTime - $nowTime;

//2.判断距离范围
        //订单的地理位置
        $orderLocation = $order['qorder_address'] . $order['qorder_d'];
        $distance = (float) $this->juli($orderLocation, $shop['shop_coorp']);
        if ($distance > $validDistance || empty($distance)) {
            continue;
        }
        $order['qorder_distance'] = $distance;

//3.师傅经营范围
        //订单所属产品类目(三级商品)
        $threeTagArray = explode(",", $order['qorder_mprice']);

//通过 motorcycle 表 查找 一级类目
        $firstTagNames = [];
        foreach ($threeTagArray as $threeTag) {
            $firstTag = M("motorcycle")->where('motorcycle_id=' . $threeTag)->getField('motorcycle_product_type');
            $firstTagName = M('main_tag')->where('main_tag_id=' . $firstTag)->getField('main_tag_title');
            array_push($firstTagNames, $firstTagName);
        }
//判断两个数组是否有交集
        $intersectArry = array_intersect($shop_ppa_array, $firstTagNames);
        if (empty($intersectArry)) {
            continue;
        }

        array_push($orderList, $order);
    }

    $count = count($orderList);

    if (!empty($orderList)) {
        $this->myApiPrint('请求成功！', 200, $orderList, $shop, $count, $message_count);
    } else {
        $this->myApiPrint('请求失败！', 300, $orderList, $shop, $count, $message_count, $shop_ppa_array);
    }

}